<?php
class Student_db extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    function get_meta() {
        $meta = array(
            "table"=>"student",
            "fields" => array(
               "reg_no" => array(
                    "label"=>"Reg. No.",
                    "type"=>"number"
               ),
               "reg_date" => array(
                    "label"=>"Reg. Date",
                    "type"=>"date"
               ),
               "student_joining_date" => array(
                    "label"=>"Joining Date",
                    "type"=>"date"
               ),
               "student_name" => array(
                    "label"=>"Name",
                    "type"=>"string"
               ),
               "student_gender" => array(
                    "label"=>"Gender",
                    "type"=>"enum",
                    "enum"=>array("m"=>"Male","f"=>"Female")
               ),
               "student_dob" => array(
                    "label"=>"Date of Birth",
                    "type"=>"date"
               ),
               "student_nationality" => array(
                    "label"=>"Nationality",
                    "type"=>"ref",
                    "ref"=>array(
                        "table"=>"country",
                        "pk"=>"id",
                        "value"=>"name"
                    )
               ),
               "student_image" => array(
                    "label"=>"Image",
                    "type"=>"string"
               ),
               "student_rp_no" => array(
                    "label"=>"RP No.",
                    "type"=>"string"
               ),
               "student_rp_expiry" => array(
                    "label"=>"RP Expiry",
                    "type"=>"date"
               ),
               "student_father_name" => array(
                    "label"=>"Father's Name",
                    "type"=>"string"
               ),
               "student_father_rp_no" => array(
                    "label"=>"Father's RP No.",
                    "type"=>"string"
               ),
               "student_mother_name" => array(
                    "label"=>"Mother's Name",
                    "type"=>"string"
               ),
               "student_address" => array(
                    "label"=>"Address",
                    "type"=>"string"
               ),
               "student_res_area" => array(
                    "label"=>"Area",
                    "type"=>"ref",
                    "ref"=>array(
                        "table"=>"area",
                        "pk"=>"area_id",
                        "value"=>"area_name"
                    )
               ),
               "student_phone_mob" => array(
                    "label"=>"Mobile",
                    "type"=>"string"
               ),
               "student_phone_office" => array(
                    "label"=>"Telephone (Office)",
                    "type"=>"string"
               ),
               "student_phone_res" => array(
                    "label"=>"Telephone (Residence)",
                    "type"=>"string"
               ),
               "student_email" => array(
                    "label"=>"Email",
                    "type"=>"string"
               ),
               "student_father_workplace" => array(
                    "label"=>"Father's work place",
                    "type"=>"string"
               ),
               "student_allergy" => array(
                    "label"=>"Allergies",
                    "type"=>"string"
               ),
               "student_left_handed" => array(
                    "label"=>"Left Handed",
                    "type"=>"boolean"
               ),
               "student_class" => array(
                    "label"=>"Class",
                    "type"=>"ref",
                    "ref"=>array(
                        "table"=>"class",
                        "pk"=>"class_id",
                        "value"=>"class_label"
                    )
               ),
               "student_division" => array(
                    "label"=>"Division",
                    "type"=>"ref",
                    "ref"=>array(
                        "table"=>"division",
                        "pk"=>"division_id",
                        "value"=>"division_label"
                    )
               ),
               "student_status" => array(
                    "label"=>"Status",
                    "type"=>"ref",
                    "ref"=>array(
                        "table"=>"studentstatus",
                        "pk"=>"student_status_id",
                        "value"=>"student_status_text"
                    )
               ),
               "student_comment" => array(
                    "label"=>"Comments",
                    "type"=>"string"
               ),
               "student_leaving_date" => array(
                    "label"=>"Date of Leaving",
                    "type"=>"date"
               ),
               "student_leaving_reason" => array(
                    "label"=>"Reason for leaving",
                    "type"=>"string"
               )
            )
        );
        
        return $meta;
    }
    
    
    function save_student($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('student', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('reg_no', $pk);
    		$this->db->update('student', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
    function get_student($data, $select="") {
        if($select!="") {
            $this->db->select($select);
        }
        $this->db->from('student st');
        $this->db->join('country ct','st.student_nationality = ct.id','left');
        $this->db->join('area ar','st.student_res_area = ar.area_id','left');
        $this->db->join('class cl','st.student_class = cl.class_id','left');
        $this->db->join('division di','st.student_division = di.division_id','left');
        $this->db->join('studentstatus ss','st.student_status = ss.student_status_id','left');
        $this->db->where($data);
        $query = $this->db->get();
        $result = $query->result_array();
        //print_object($result); exit;
        return $result;
    }
    
    function get_student_status_list() {
        $query = $this->db->get('studentstatus');
        $result = format_list($query->result_array(),"student_status_id","student_status_text");
        return $result;
    }
    
    function get_studentlist($student_type = '') {
        if($student_type != '')
        $this->db->where('student_type_id', $student_type);
        
        $query = $this->db->get('student');
        $result = $query->result_array();
        return $result;
    }
    
    function delete_student($data) {
        $this->db->where($data);
        $result = $this->db->update('student', array('student_status' => 4));
        //print_object($result);exit;
        //print_object($this->db->last_query());exit;
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }
    
    function update_student_image($reg_no, $img_src) {
        $this->db->where(array('reg_no'=> $reg_no));
        $result = $this->db->update('student', array('student_image' => $img_src));
        //print_object($result);exit;
        //print_object($this->db->last_query());exit;
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }
    
    function get_student_filter_count($filterdata='',$include_deleted=true) {
        $this->db->from('student st');
        $this->db->join('class cls','st.student_class = cls.class_id','left');
        $this->db->join('division dv','st.student_division = dv.division_id','left');
        $this->db->join('studentstatus ss','st.student_status = ss.student_status_id','left');
        $this->db->join('country ct','st.student_nationality = ct.id','left');
        $this->db->join('area ar','st.student_res_area = ar.area_id','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
        
        if(!$include_deleted) {
            $this->db->where('st.student_status !=',4);
        }
        
        return $this->db->count_all_results();
    }
    
    function get_student_filter_list($limit=0,$start=0,$filterdata='',$include_deleted=true) {
        $this->db->from('student st');
        $this->db->join('class cls','st.student_class = cls.class_id','left');
        $this->db->join('division dv','st.student_division = dv.division_id','left');
        $this->db->join('studentstatus ss','st.student_status = ss.student_status_id','left');
        $this->db->join('country ct','st.student_nationality = ct.id','left');
        $this->db->join('area ar','st.student_res_area = ar.area_id','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
        
        if(!$include_deleted) {
            $this->db->where('st.student_status !=',4);
        }
                
        $this->db->order_by("st.reg_no", "asc");
        $this->db->limit($limit, $start); 
        $query = $this->db->get();
        //print_object($this->db->last_query());
        return $query->result_array();
    }
    
    function add_dg_col($col,$meta, &$cols) {
        //'reg_no'=>array('label'=>'Reg. No.','type'=>'link','visible'=>true, 'href'=>'/student/viewstudent')
        $label = $meta['label'];
        $type = $meta['type'];
        $enum = array();
        if($type == 'enum') {
            $enum = $meta['enum'];
        }
        
        if($type == 'ref') {
            $type = "string";
            $col = $meta['ref']['value'];
        }
        
        $cols[$col] = array('label'=>$label,'type'=>$type, 'enum'=>$enum,'visible'=>true);
    }

    
    function build_student_dg_data($fiterdata='') {
        $result['dg_title'] = "Student List";
        if($this->input->post('show_all')) {
            $result['show_all'] = true;
        } else {
            //$result['show_all'] = false;
            $result['current_page'] = ($this->input->post('page')) ? $this->input->post('page') : 1;
            $result['rows_per_page'] = ($this->config->item('per_page')) ? $this->config->item('per_page') : 10;
        }
        if($fiterdata=='')
            $result['total'] = $this->get_student_filter_count();
        else
            $result['total'] = $this->get_student_filter_count($fiterdata);
            
        if($result['show_all']) {
            $result['current_page'] = 1;
            $result['rows_per_page'] = ($result['total']==0)?100:$result['total'];
        }
        
        $result['pk'] = 'reg_no';
        $result['itemname'] = 'student_name';
        //print_object($fiterdata); exit;
        if(is_array($fiterdata['cols']) && count($fiterdata['cols']) > 0) {
            $dgcols = array(
                'reg_no'=>array('label'=>'Reg. No.','type'=>'link','visible'=>true, 'href'=>'/student/viewstudent')
            );
            foreach($fiterdata['cols'] as $col) {
                $dgcol = $this->add_dg_col($col,$fiterdata['meta']['fields'][$col],$dgcols);
            }
            $result['dgcolumns'] = $dgcols;
        } else {
            $result['dgcolumns'] = array(
                'reg_no'=>array('label'=>'Reg. No.','type'=>'link','visible'=>true, 'href'=>'/student/viewstudent'),
                'student_name'=>array('label'=>'Name','type'=>'string','visible'=>true),
                'class_label'=>array('label'=>'Class','type'=>'string','visible'=>true),
                'division_label'=>array('label'=>'Division','type'=>'string','visible'=>true),
                'student_status_text'=>array('label'=>'Status','type'=>'string','visible'=>true),
                'fee_status'=>array('label'=>'Fee Status','type'=>'string','visible'=>true), 
                'attendance_status'=>array('label'=>'Attendance Status','type'=>'string','visible'=>true),
                'report_card'=>array('label'=>'Report Card','type'=>'string','visible'=>true)           
            );
        }        
        
        $result['actions'] = array(
        'add'=>'/student/addstudent',
        'edit'=>'/student/editstudent',
        'delete'=>'/student/deletestudent',
        'view'=>'/student/viewstudent',
        'page'=>'/student/studentlist'
        );
        
        $offset = $result['rows_per_page']*($result['current_page']-1);
        $result['pages_total'] =  ceil($result['total']/$result['rows_per_page']);
        if($fiterdata=='')
            $result["dgitems"] = $this->get_student_filter_list($result['rows_per_page'], $offset, '');
        else
            $result["dgitems"] = $this->get_student_filter_list($result['rows_per_page'], $offset, $fiterdata);
        return $result;
    }
    
}

?>